//
//  SLHomeController.swift
//  Sina15
//
//  Created by 盼 on 16/4/6.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SVProgressHUD
import SnapKit

private let homeCellId = "homeCell"

class SLHomeController: SLBaseTableViewController {
    
    //模型数组
//    lazy var statuses:[SLStatus] = [SLStatus]()
    
    //自定义刷新控件
    var freshControl: SLRefreshControl?
    
    //加载
    private lazy var indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    
    //给控制器提供一个viewModel模型
    lazy var statusListViewModel = SLStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
            
        setupHomeInfo()
        
        //加载3964619200166563
        loadData()
    }
    
    private func setupHomeInfo(){
    
        visitorView?.setupViewInfo(true,title:"关注一些人,回到这里看看有什么惊喜",imageName:"visitordiscover_feed_image_smallicon")
        
        if userLogin == true {
            
            //设置导航条右侧的item
            navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "push")
            
            //设置左侧的item
            navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch",target: self, action: "push")

            prepareTableView()
        }
        
    }

    
    //准备tableView的界面
    func prepareTableView(){
        
        //取消分割线
        tableView.separatorStyle = .None
        
        //设置行高为自动计算
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //设置预估行高
        tableView.estimatedRowHeight = 300
        
        tableView.registerClass(SLStatusCell.self, forCellReuseIdentifier: homeCellId)
        
        tableView.tableFooterView = indicatorView
        
        //设置UITableView的refreshControl
        let freshControl = SLRefreshControl()
        
        self.freshControl = freshControl
        
        //添加刷新控件
        tableView.addSubview(freshControl)
        
        //给刷新控件添加刷新数据的触发事件
        freshControl.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)

    }
    
    //按钮的点击事件
   @objc private func push(){
    
    print("push")
    
    let tempVC = SLTempViewController()
    
    navigationController?.pushViewController(tempVC, animated: true)
    
    }
    
    
    //加载数据
    func loadData(){
        
        statusListViewModel.loadHomePage(indicatorView.isAnimating()) { (isSuccess) -> () in
        
            //不管成功还是失败都应该停止freshControl的转动
            self.freshControl?.endRefreshing()
            
            if !isSuccess {
                
                SVProgressHUD.showErrorWithStatus("加载微博数据失败")
                
                return
            }
            
            //数据加载成功后停止菊花转动
            self.indicatorView.stopAnimating()

            //加载数据成功，刷新数据
            self.tableView.reloadData()
        }
    }
}


//MARK:实现数据源方法
extension SLHomeController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.statusModels.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //创建cell,并把cell转换成SLStatusCell类型
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as! SLStatusCell
        
        let statusModel = statusListViewModel.statusModels[indexPath.row]
        
        //给cell设置数据
        cell.statusModel = statusModel
        
        //上拉加载更多数据
        if indexPath.row == statusListViewModel.statusModels.count - 1 && !indicatorView.isAnimating(){
            
            indicatorView.startAnimating()
//            performSelector("loadData", withObject: nil, afterDelay: 2)
            
            loadData()
        }
        
        return cell
    }
}
